/**
 * Created by zhenghao on 2016/10/12.
 */
var $description = document.getElementById("description"),
    $videoLoading = $("#loading"),
    $errorTxt = $("#errorTxt"),
    $errorVal = $("#errorTxt span"),
    screenWidth = document.documentElement.clientWidth,
    $descriptionContent = $("#descriptionContent"),
    intervalTime = 6e4,
    interval,
    videoPlay = {
        retry: 3,
        defaults: {
            picSrc: "/page/videoPlay/img/poster.jpg"
        },
        init: function() {
            this.checkRetCode()
        },
        checkRetCode: function() {
            var target = this;
            switch (
                    $(".videoContent").height(screenWidth * (9 / 16)),
                    $descriptionContent.html(intro.replace(/ /g, "&nbsp;")),
                    $description.style.minHeight = document.documentElement.clientHeight - screenWidth * (9 / 16) + "px",
                    Number(retcode)
                ) {
                case 1010:
                    target.showError("参数错误");
                    break;
                case 1130:
                    target.showError("直播服务尚未启用");
                    break;
                case 1131:
                    target.showError("未找到播放信息,请查看地址是否正确");
                    break;
                case 1132:
                    target.showError("设备没有关联地址,请联系拥有者开通直播服务");
                    break;
                case 1133:
                    target.showError("当前日期不可观看");
                    break;
                case 1134:
                    target.showError("当前时间不可观看");
                    break;
                case 0:
                    //if (this.checkDuration()) return;     //检查有效期
                    //passwordEnable && password && $.fn.cookie(addressId + "-password") != password ? target.setPassword() : target.setVideo();   //检查密码

                    target.setVideo();
                    break;
                default:
                    target.showError("网络错误，请稍后重试...")
            }
        },
        setVideo: function() {
            var target = this,
                content = '<video src="' + playUrl + '" poster="'+cover+'" controls="controls" webkit-playsinline=""  id="videoPlay"></video>';
            $(".videoContent").append(content);
            var video = document.getElementById("videoPlay");
            video.style.height = screenWidth * (9 / 16) + "px",
            cover && (video.poster = cover),
                video.addEventListener("waiting",
                    function() {
                        console.log("waiting"),
                            $videoLoading.removeClass("hide")
                    },
                    !1),
                video.addEventListener("canplaythrough",
                    function() {
                        console.log("canplaythrough"),
                            $videoLoading.addClass("hide")
                    },
                    !1),
                video.addEventListener("error",
                    function() {
                        console.log("error"),
                            videoPlay.retry > 0 ? (video.load(), video.play()) : target.showError("网络繁忙，请刷新页面重试"),
                            videoPlay.retry--
                    },
                    !1),
                this.checkLimit()
        },
        setPassword: function() {
            var target = this,
                $markLi = $(".mark li"),
                $pointInput = $(".pointValue");
            $(".passContainer").removeClass("hide");
            for (var $point = document.getElementsByClassName("point"), i = 0; i < $point.length; i++) $point[i].style.height = $point[i].offsetWidth / 100 + "rem";
            $(".passContainer").on("touchstart", ".point",
                function(e) {
                    var $this = $(this),
                        value = $this.attr("value"),
                        $markOn = $(".mark .on");
                    if ($markOn.length) {
                        if ($markOn.last().next().addClass("on"), 6 === $markOn.length) return;
                        5 === $markOn.length ? ($markOn.last().next().addClass("on"), $pointInput.val($(".pointValue").val() + value), $(".pointValue").val() == password ? (setTimeout(function() {
                                $(".passContainer").addClass("hide")
                            },
                            200), $pointInput.val(""), $markLi.removeClass("on"), target.setVideo(), $.fn.cookie(addressId + "-password", password, {
                            expires: 7
                        })) : (target.tipError("密码错误，请重新输入"), $pointInput.val(""), $markLi.removeClass("on"))) : ($pointInput.val($(".pointValue").val() + value), $markOn.next().addClass("on"))
                    } else $markLi.eq(0).addClass("on"),
                        $pointInput.val($(".pointValue").val() + value);
                    $this.removeClass("active"),
                        setTimeout(function() {
                                $this.addClass("active")
                            },
                            0)
                }),
                $(".passContainer").on("touchend", ".point",
                    function(e) {
                        $(this)
                    }),
                $(".passContainer").on("touchstart", ".deleteBtn",
                    function(e) {
                        var $markOn = ($(this), $(".mark .on")),
                            value = $pointInput.val();
                        value = value.substring(0, value.length - 1),
                            $pointInput.val(value),
                        $markOn.length && $markOn.last().removeClass("on")
                    })
        },
        showError: function(string) {
            string && $errorVal.html(string),
                $errorTxt.removeClass("hide")
        },
        tipError: function(error) {
            error && $(".passContainer .title").html(error)
        },
        checkDuration: function() {
            var target = this;
            if (duration && durationEnable) {
                var record = Number($.fn.cookie(addressId + "-duration"));
                if (record > duration) return target.showError("已达到每日观看时长"),
                    !0
            }
        },
        checkLimit: function() {
            function cal() {
                var now = new Date(Number(serverTime) + intervalTime);
                if (week && weekEnable) {
                    var weekLimitArr = week.split(",");
                    if (weekLimitArr.unshift(weekLimitArr.pop()), "0" === weekLimitArr[now.getDay()]) return target.showError("当前在限制观看日期内"),
                        void clearInterval(interval)
                }
                if (time && timeEnable) {
                    var arr = time.split(/:|-/),
                        sTime = new Date;
                    sTime.setHours(Number(arr[0])),
                        sTime.setMinutes(Number(arr[1]));
                    var eTime = new Date;
                    if (eTime.setHours(Number(arr[2])), eTime.setMinutes(Number(arr[3])), now.getTime() < sTime.getTime() || now.getTime() > eTime.getTime()) return target.showError("当前在限制观看时间内"),
                        void clearInterval(interval)
                }
                if (duration && durationEnable) {
                    var oneDay = 864e5,
                        durationEndTime = new Date(Number(serverTime));
                    durationEndTime.setHours(0),
                        durationEndTime.setMinutes(0),
                        durationEndTime.setSeconds(0),
                        durationEndTime.setTime(durationEndTime.getTime() + oneDay);
                    var expires = (durationEndTime.getTime() - now.getTime()) / oneDay,
                        record = Number($.fn.cookie(addressId + "-duration"));
                    record ? record += 60 : record = 60,
                    record > duration && (target.showError("已达到每日观看时长"), clearInterval(interval)),
                        $.fn.cookie(addressId + "-duration", record, {
                            expires: expires
                        })
                }
            }
            var target = this; (week && weekEnable || time && timeEnable || duration && durationEnable) && (interval = setInterval(cal, intervalTime))
        }
    };
videoPlay.init();